package org.springblade.modules.platform.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.modules.platform.dto.MeasuringPlatformTypeDTO;
import org.springblade.modules.platform.entity.DeviceTable;
import org.springblade.modules.platform.entity.MeasuringPlatformConfig;
import org.springblade.modules.platform.entity.MeasuringPlatformType;
import org.springblade.modules.platform.entity.SysParameter;
import org.springblade.modules.platform.mapper.DeviceTableMapper;
import org.springblade.modules.platform.mapper.MeasuringPlatformConfigMapper;
import org.springblade.modules.platform.mapper.MeasuringPlatformTypeMapper;
import org.springblade.modules.platform.mapper.SysParameterMapper;
import org.springblade.modules.platform.service.IMeasuringPlatformTypeService;
import org.springblade.modules.platform.vo.MeasuringPlatformConfigVO;
import org.springblade.modules.platform.vo.MeasuringPlatformTypeVO;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author SYG
 * @since 2023-06-29
 */
@Service
public class MeasuringPlatformTypeServiceImpl extends ServiceImpl<MeasuringPlatformTypeMapper, MeasuringPlatformType> implements IMeasuringPlatformTypeService {

    @Autowired
    DeviceTableMapper deviceTableMapper;
    @Autowired
    MeasuringPlatformConfigMapper measuringPlatformConfigMapper;
    @Autowired
    SysParameterMapper sysParameterMapper;


    private static final List<String> BASISLIST = Arrays.asList("GAMA","Z","ALPHA","BETA");
    private static final List<String> LIST = Arrays.asList("DELTAY","DIPX","DIPY","NX","NZ","ZX","ZZ");

    @Override
    public void insertMeasuringPlatformType(MeasuringPlatformTypeDTO measuringPlatformTypeDTO) {
        MeasuringPlatformType copy = BeanUtil.copy(measuringPlatformTypeDTO, MeasuringPlatformType.class);
        copy.setIsDeleted(0);
        copy.setInsertTime(System.currentTimeMillis());
        copy.setUpdateTime(System.currentTimeMillis());
        List<DeviceTable> deviceTables = deviceTableMapper.selectList(Wrappers.<DeviceTable>query().lambda()
                .in(DeviceTable::getDeviceId, measuringPlatformTypeDTO.getDeviceId().split(","))
                .eq(DeviceTable::getIsDeleted, 0));
        if(!Func.isEmpty(deviceTables)){
            String collect = deviceTables.stream().map(DeviceTable::getDeviceName).collect(Collectors.joining(", "));
            copy.setDeviceName(collect);
        }
        baseMapper.insert(copy);

        Integer measuringPlatformType = measuringPlatformTypeDTO.getMeasuringPlatformType();
        Long id = copy.getId();


        if(measuringPlatformType==1){
            BASISLIST.forEach(x->{
                SysParameter sysParameter = new SysParameter();
                sysParameter.setMeasuringPlatformId(id);
                sysParameter.setDeviceChannelId(copy.getDeviceChannelId());
                sysParameter.setDeviceName(copy.getDeviceName());
                sysParameter.setParameterKey(x);
                sysParameter.setMinValue(0.0);
                sysParameter.setMaxValue(7500.0);
                sysParameter.setDipXK(0.0047);
                sysParameter.setDipXB(0.0);
                sysParameter.setInsertTime(LocalDateTime.now());
                sysParameter.setInsertTime(LocalDateTime.now());
                sysParameter.setIsDeleted(0);
                sysParameterMapper.insert(sysParameter);
            });
        }else {
            LIST.forEach(x->{
                SysParameter sysParameter = new SysParameter();
                sysParameter.setMeasuringPlatformId(id);
                sysParameter.setDeviceChannelId(copy.getDeviceChannelId());
                sysParameter.setDeviceName(copy.getDeviceName());
                sysParameter.setParameterKey(x);
                sysParameter.setMinValue(0.0);
                sysParameter.setMaxValue(7500.0);
                sysParameter.setDipXK(0.0047);
                sysParameter.setDipXB(0.0);
                sysParameter.setInsertTime(LocalDateTime.now());
                sysParameter.setInsertTime(LocalDateTime.now());
                sysParameter.setIsDeleted(0);
                sysParameterMapper.insert(sysParameter);
            });
        }


    }

    @Override
    public void updateMeasuringPlatformType(MeasuringPlatformTypeDTO measuringPlatformTypeDTO) {
        MeasuringPlatformType copy = BeanUtil.copy(measuringPlatformTypeDTO, MeasuringPlatformType.class);
        copy.setIsDeleted(0);
        copy.setUpdateTime(System.currentTimeMillis());
        baseMapper.updateById(copy);
    }

    @Override
    public void deleteMeasuringPlatformType(Long id) {
        MeasuringPlatformType measuringPlatformType = baseMapper.selectById(id);
        measuringPlatformType.setIsDeleted(1);
        measuringPlatformType.setUpdateTime(System.currentTimeMillis());
        baseMapper.updateById(measuringPlatformType);

    }
    @Override
    public MeasuringPlatformTypeVO deileMeasuringPlatformType2(Long id) {
        MeasuringPlatformType measuringPlatformType = baseMapper.selectById(id);
        MeasuringPlatformTypeVO copy1 = BeanUtil.copy(measuringPlatformType, MeasuringPlatformTypeVO.class);

        List<MeasuringPlatformConfig> measuringPlatformConfigs = measuringPlatformConfigMapper.selectList(Wrappers.<MeasuringPlatformConfig>query().lambda()
                .eq(MeasuringPlatformConfig::getMeasuringPlatformId, id));
        if(Func.isEmpty(measuringPlatformConfigs)){
            return copy1;
        }
        List<MeasuringPlatformConfigVO> collect = measuringPlatformConfigs.stream().map(x -> BeanUtil.copy(x, MeasuringPlatformConfigVO.class)).collect(Collectors.toList());
        copy1.setList(collect);
        return copy1;
    }
    @Override
    public MeasuringPlatformTypeVO deileMeasuringPlatformType(Long id, Integer measuringTypeCode) {
        MeasuringPlatformType measuringPlatformType = baseMapper.selectById(id);
        MeasuringPlatformTypeVO copy1 = BeanUtil.copy(measuringPlatformType, MeasuringPlatformTypeVO.class);

        List<MeasuringPlatformConfig> measuringPlatformConfigs = measuringPlatformConfigMapper.selectList(Wrappers.<MeasuringPlatformConfig>query().lambda()
                .eq(MeasuringPlatformConfig::getMeasuringPlatformId, id)
                .eq(MeasuringPlatformConfig::getPlatformTypeCode, measuringTypeCode));
        if(Func.isEmpty(measuringPlatformConfigs)){
            return copy1;
        }
        List<MeasuringPlatformConfigVO> collect = measuringPlatformConfigs.stream().map(x -> BeanUtil.copy(x, MeasuringPlatformConfigVO.class)).collect(Collectors.toList());
        copy1.setList(collect);
        return copy1;
    }
}
